const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin, 
  output: process.stdout
});

function multiPack(c, n, weights, values, nums) {
  const dp = new Array(c + 1).fill(0);


  // 先物品
  for (let i = 0; i < n; i++) {
    // 再背包  01背包 -  后往前
    for (let j = c; j >= weights[i]; j--) {
      // 物品数量
      for (let k = 1; k <= nums[i] && j - weights[i] * k >= 0; k++) {
        dp[j] = Math.max(dp[j], dp[j - weights[i] * k] + k * values[i]);
      }
    }
  }

  return dp[c];
}

const c = 10, n = 3;
const weights = [1, 3,4]
values =  [15, 20, 30]
nums = [2,3,2]

console.log(multiPack(c, n, weights, values, nums));
rl.close();


const input = [];
rl.on('line', (line) => {
  input.push(line.split(' ').map(Number));
})
rl.on('close', () => {
  const [c, n] = input.shift();
  const weights = input.shift();
  const values = input.shift();
  const nums = input.shift();
  console.log(multiPack(c, n, weights, values, nums));
})